﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Drawing.Text;
using System.IO;
using System.Linq;
using System.Web;

namespace iemtm.WeChat.Core.Extension.Helper
{
   

    public class ImageHelper
    {
        /// <summary>
        /// base64 图片-加入文字 --返回base64 图片字符串
        /// </summary>
        /// <param name="base64string">base64 图片</param>
        /// <param name="Content">加入的文字</param>
        /// <returns></returns>
        public static string GetImageFromBase64InToContent(string base64string,string Content)
        {
            var T = GetImageFromBase64(base64string);
            var TT=AddTextToImg(T, Content);
            var TTT = GetBase64FromImage(TT);
            return TTT;
        }

        /// <summary>
        /// 根据base64字符串返回一个封装好的GDI+位图。
        /// </summary>
        /// <param name="base64string">可转换成位图的base64字符串。</param>
        /// <returns>Bitmap对象。</returns>
        public static Bitmap GetImageFromBase64(string base64string)
        {
            byte[] b = Convert.FromBase64String(base64string);
            MemoryStream ms = new MemoryStream(b);
            Bitmap bitmap = new Bitmap(ms);
            return bitmap;
        }



        /// <summary>
        /// GDI+位图图片转换成base64字符串。
        /// </summary>
        /// <param name="imagefile">需要转换的图片文件。</param>
        /// <returns>base64字符串。</returns>
        public static string GetBase64FromImage(Bitmap bmp)
        {
            string strbaser64 = "";

            try
            {
                // Bitmap bmp = new Bitmap(imagefile);
                using (MemoryStream ms = new MemoryStream())
                {
                    bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                    byte[] arr = new byte[ms.Length];
                    ms.Position = 0;
                    ms.Read(arr, 0, (int)ms.Length);
                    ms.Close();

                    strbaser64 = Convert.ToBase64String(arr);
                }
            }
            catch (Exception)
            {
                throw new Exception("Something wrong during convert!");
            }

            return strbaser64;
        }


        /// <summary>
        /// 图片加文字
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="text"></param>
        public static Bitmap AddTextToImg(Bitmap bmp, string text)
        {
            //if (!File.Exists(MapPath(fileName)))
            //{
            //    throw new FileNotFoundException("The file don't exist!");
            //}

            //if (text == string.Empty)
            //{
            //    return;
            //}
            //还需要判断文件类型是否为图像类型，这里就不赘述了

            //System.Drawing.Image image = System.Drawing.Image.FromFile(MapPath(fileName));
            System.Drawing.Image image = bmp;
            Bitmap bitmap = new Bitmap(image, image.Width, image.Height);
            Graphics g = Graphics.FromImage(bitmap);

            float fontSize = 24.0f;    //字体大小
            float textWidth = text.Length * fontSize;  //文本的长度
                                                       //下面定义一个矩形区域，以后在这个矩形里画上白底黑字
            float rectX = 0;
            float rectY = 0;
            float rectWidth = text.Length * (fontSize + 8);
            float rectHeight = fontSize + 8;
            //声明矩形域
            //RectangleF textArea = new RectangleF(rectX, rectY, rectWidth, rectHeight);

            Font font = new Font("宋体", fontSize);   //定义字体
            //Brush whiteBrush = new SolidBrush(Color.White);   //白笔刷，画文字用
            //Brush blackBrush = new SolidBrush(Color.Black);   //黑笔刷，画背景用

            //g.FillRectangle(blackBrush, rectX, rectY, rectWidth, rectHeight);

            //g.DrawString(text, font, whiteBrush, textArea);
            #region 写字
            g.SmoothingMode = SmoothingMode.AntiAlias; //使绘图质量最高，即消除锯齿
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            g.CompositingQuality = CompositingQuality.HighQuality;
            g.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
            //  fg.FillPie(new SolidBrush(Color.Yellow), new Rectangle(0, 0, size - 1, size - 1), 0, 360);
            g.SmoothingMode = SmoothingMode.Default;
            g.InterpolationMode = InterpolationMode.Default;
            g.CompositingQuality = CompositingQuality.Default;
            StringFormat stringFormat = new StringFormat();
            stringFormat.Alignment = StringAlignment.Center;
            g.DrawString(text, font, new SolidBrush(Color.Yellow), image.Width - 345, image.Height - 100);
            #endregion
            MemoryStream ms = new MemoryStream();
            //保存为Jpg类型
            bitmap.Save(ms, ImageFormat.Jpeg);

            //输出处理后的图像，这里为了演示方便，我将图片显示在页面中了
            //Response.Clear();
            //Response.ContentType = "image/jpeg";
            //Response.BinaryWrite(ms.ToArray());

            //g.Dispose();
            //bitmap.Dispose();
            //image.Dispose();
            return bitmap;
        }


        public static Bitmap Byte2Bitmap(byte[] bytes)
        {
            byte[] bytelist = bytes;
            Bitmap bitmap;
            using (MemoryStream ms1 = new MemoryStream(bytelist))
            {
                bitmap = (Bitmap)Image.FromStream(ms1);
                ms1.Close();
            }
            return bitmap;
        }



    }
}
